package com.zx.dao.impl;

import com.zx.bean.Treat;
import com.zx.dao.TreatDao;
import com.zx.util.DruidUtil;

import java.sql.*;

public class TreatDaoImpl implements TreatDao {
    public static final String SQL_INSERT="insert into treat (timelong,symptom,suggest,name,treattime) values(?,?,?,?,?)";
    public static final String SQL_FIND_BY_NAME="select * from treat where name=? ";
    @Override
    public boolean insert(String timelong, String symptopm, String suggest, String name,String treatTime) {
        //1 连接的获取
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement =null;

        try {
            //2预编译SQL
            preparedStatement=connection.prepareStatement(SQL_INSERT);
            //3 填充参数
            preparedStatement.setString(1,timelong);
            preparedStatement.setString(2,symptopm);
            preparedStatement.setString(3,suggest);
            preparedStatement.setString(4,name);
            preparedStatement.setString(5,treatTime);
            //4 执行SQL获取执行结果
            return  preparedStatement.executeUpdate()>0?true:false;
            //6 释放资源
        } catch (SQLException e1) {
            System.out.println(e1.getStackTrace());
        } finally {
            DruidUtil.close(connection,preparedStatement, null);
        }

        return false;

    }

    @Override
    public Treat findByName(String name) {
        Connection connection=DruidUtil.getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;


        try {
            preparedStatement=connection.prepareStatement(SQL_FIND_BY_NAME);
            preparedStatement.setString(1,name);
            resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                int id=resultSet.getInt("id");
                String timelong=resultSet.getString("timelong");
                String symptom=resultSet.getString("symptom");
                String suggest=resultSet.getString("suggest");
                String treatTime=resultSet.getString("treatTime");
                Treat treat=new Treat();
                treat.setTimelong(timelong);
                treat.setSymptom(symptom);
                treat.setSuggest(suggest);
                treat.setTreatTime(treatTime);
                return treat;


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }
        return null;
    }
}
